﻿using System;
using System.Data.SqlServerCe;
using System.IO;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace MadMediaManager.DataAccess.Database
{
    public class SqlCeDatabaseCreator
    {
        private string _connectionString;
        public SqlCeDatabaseCreator(string connectionString)
        {
            _connectionString = connectionString;
        }

        public SqlCeDatabaseCreator Create()
        {
            using (var sqlEngine = new SqlCeEngine(_connectionString))
            {
                sqlEngine.CreateDatabase();
            }

            return this;
        }

        public SqlCeDatabaseCreator Delete()
        {
            using (var conneection = new SqlCeConnection(_connectionString))
            {
                File.Delete(conneection.Database);
            }

            return this;
        }

        public SqlCeDatabaseCreator Script(Configuration configuration)
        {
            using (var conneection = new SqlCeConnection(_connectionString))
            {
                var export = new SchemaExport(configuration);

                conneection.Open();
                export.Execute(null, true, false, conneection, Console.Out);
                conneection.Close();
            }

            return this;
        }
    }
}
